package com.mujin.keji.collectionsystem.module.main.views;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.CcsApplication;
import com.mujin.keji.collectionsystem.Constant;
import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.base.BaseActivity;
import com.mujin.keji.collectionsystem.module.login.views.LoginActivity;
import com.mujin.keji.collectionsystem.server.RecordServcie;
import com.mujin.keji.collectionsystem.utils.FileUtil;
import com.mujin.keji.collectionsystem.utils.SPUtil;


/**
 * 重新登录 Activity
 *
 * @author weishixiong
 * @Time 2018-06-25
 */
public class ReloginActivity extends BaseActivity {
    private TextView tvOk;
    private TextView tvContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CcsApplication.addActivity(this);
        //重新登录的时候 把 服务停掉
        Intent intentService = new Intent(this, RecordServcie.class);
        stopService(intentService);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CcsApplication.removeActivity(this);
    }

    @Override
    protected void createViewModel() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_relogin;
    }

    @Override
    public void initView() {
        tvOk = (TextView) findViewById(R.id.tv_ok);
        tvContent = (TextView) findViewById(R.id.tv_content);
    }

    @Override
    public void initData() {
        tvContent.setText(getIntent().getStringExtra("msg"));
    }

    @Override
    public void initListener() {
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                relogin();
            }
        });
    }

    /**
     * 重新登录
     */
    private void relogin() {
        SPUtil.clearData(Constant.SP.SP);
        FileUtil.clearAllCache(CcsApplication.getInstance());
        Intent intent = new Intent(ReloginActivity.this, LoginActivity.class);
        startActivity(intent);
        Constant.Server.isRelogin = false;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            relogin();
        }
        return false;
    }

}
